<template>
  <div style="height: 100%;background-color:white ;">
    <div>
      <div style="display: inline-block">
        <el-button icon="el-icon-back" size="small" style="font-family: Microsoft YaHei" type="text"
                   @click="goBack">返回
        </el-button>
      </div>
      <div style="display: inline-block">|</div>
      <div style="display: inline-block;">
        <div style="padding-bottom: 15px;margin-left:10px">
          <span style="font-family: Microsoft YaHei;display: inline-block; font-size: 14px;">推送</span>
        </div>
      </div>
      <div style="border-bottom: solid 1px #C9C9C9;margin-bottom: 5px"></div>
    </div>
    <div>
      <el-row>
        <el-col :span="12">
          <el-button type="success" size="small" @click="syncSelectedKnowledge" plain>知识推送</el-button>
          <el-button type="success" size="small" @click="syncWaitAndFailedKnowledge" plain>一键推送</el-button>
          <el-button type="success" size="small" @click="syncAllKnowledge" plain>全量推送</el-button>
        </el-col>
        <el-col :span="12">
          <SynchronizationSearch :id="id" ref="synchronizationSearch"
                                 :drugInfoLibStatus="drugInfoLibStatus"
                                 :isToPlatform="isToPlatform"
                                 @confirm="confirm"></SynchronizationSearch>
        </el-col>
      </el-row>
    </div>
    <el-row class="bottom-space">
      <div>
        <el-table highlight-current-row ref="table" :data="synTableData" :height="tableHeight" border stripe
                  style="width: 100%;margin-top: 10px"
                  row-key="id" @selection-change="SelectionChange">
          <el-table-column
            v-model="SelectionAll"
            align="center"
            type="selection" width="50">
          </el-table-column>
          <el-table-column label="知识名称" min-width="300" prop="name">
            <template slot-scope="scope">
              <span v-html="scope.row.name"></span>
            </template>
          </el-table-column>
          <el-table-column label="厂家"
                           min-width="100"
                           prop="drugInfoLibCompany" v-if="drugInfoLibStatus" show-overflow-tooltip></el-table-column>
          <el-table-column label="来源"
                           min-width="100"
                           prop="drugInfoLibSource" v-if="drugInfoLibStatus" show-overflow-tooltip></el-table-column>
          <el-table-column v-if="isToPlatform === 'true'"
                           label="关联平台"
                           min-width="120" prop="ruleName" show-overflow-tooltip></el-table-column>
          <el-table-column label="版本号" min-width="60" prop="version"></el-table-column>
          <el-table-column label="推送时间" min-width="130"
                           prop="operatedTime"></el-table-column>
          <el-table-column label="状态" min-width="80" prop="syncStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.syncStatus === 0" style="color: red">推送失败</span>
              <span v-else-if="scope.row.syncStatus === 1" style="color: red">消费失败</span>
              <span v-else-if="scope.row.syncStatus === 3">推送成功</span>
              <span v-else style="color: #FF8C00">待推送</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <span><el-link type="primary" @click.prevent="detail(scope.row.id)">详情</el-link></span>
              <span><el-link type="primary"
                             @click.prevent="syncSingleKnowledge(scope.row.id)">推送</el-link></span>
              <span><el-link type="primary" @click.prevent="showLog(scope.row)">日志</el-link></span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-row>
    <el-row>
      <div>
        <el-pagination :page-size="pageSize"
                       :total="totalNum"
                       :page-sizes="[5, 10, 15, 20, 50, 100]"
                       background
                       @current-change="handleCurrentChange"
                       @size-change="handleSizeChange"
                       layout="total, sizes, prev, pager, next, jumper"
                       v-if="multiplePages"
                       style="text-align: right;"></el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script>
import SynchronizationSearch from "../../knowledgeLibFiled/manage/SynchronizationSearch";

let sleep = function (time) {
  let startTime = new Date().getTime() + parseInt(time, 10);
  while (new Date().getTime() < startTime) {
  }
};
export default {
  name: "SynchronizationTable",
  components: {
    SynchronizationSearch,
  },
  data() {
    return {
      tableHeight: 0,
      synTableData: [],
      SelectionAll: [],//需要同步知识数组
      multiplePages: true,
      noData: true,
      totalNum: 0,
      currentPage: 0, // 第几页
      pageSize: 10, // 每页显示的数据个数
      knowledgeSyncStatusArray: [],
      selectedKnowledgeId: "",
      status: '',
      updateTime: '',
      editTable: [],
      knowledgeName: '',
      knowledgeId: '',
      syncToPlatformType: '', // selected 代表多选同步, waitAndFailed 代表一键同步 待同步+同步失败, all 代表全量同步;
      drugInfoLibStatus: false,
      bindingPlatform: '',
      id: '',
      isToPlatform: ''
    }
  },
  methods: {
    handleCurrentChange: function (val) {
      this.currentPage = val - 1;
      this.confirm();
    },
    detail: function (id) {
      this.$store.commit('setKnowledgeDetailId', id)
      this.$store.commit('setKnowledgeDetailStatus', 'published')
      this.$store.commit('showKnowledgeDetail')
    },
    showLog: function (row) {
      this.$router.push({
        path: '/syncLog',
        query: {id: row.id, knowledgeName: row.name}
      })
    },
    SelectionChange(val) {
      this.SelectionAll = val;
    },
    // 单条知识的推送
    syncSingleKnowledge(id) {
      this.syncToPlatformType = 'single';
      this.knowledgeId = id;
      this.syncKnowledgesToPlatforms();
    },
    syncSelectedKnowledge: function () {
      if (this.SelectionAll.length <= 0) {
        this.$message.warning("请选择知识")
        return
      }
      this.syncToPlatformType = 'selected';
      this.syncKnowledgesToPlatforms();
    },
    syncWaitAndFailedKnowledge: function () {
      this.syncToPlatformType = 'waitAndFailed'
      this.syncKnowledgesToPlatforms();
    },
    syncAllKnowledge: function () {
      this.syncToPlatformType = 'all';
      this.syncKnowledgesToPlatforms();
    },
    syncKnowledgesToPlatforms() {
      if (UTIL.isEmpty(this.$refs.synchronizationSearch.bindingPlatform)) {
        return;
      }
      let data = new URLSearchParams();
      data.append('syncToPlatformType', this.syncToPlatformType);
      data.append('knowledgeLibId', this.id);
      data.append('selectedKnowledges', JSON.stringify(this.SelectionAll));
      data.append('bindingPlatform', this.$refs.synchronizationSearch.bindingPlatform);
      data.append('knowledgeId', this.knowledgeId);
      data.append('searchParams', JSON.stringify({
        name: this.$refs.synchronizationSearch.input,
        platformId: this.$refs.synchronizationSearch.platform,
        subject: this.$refs.synchronizationSearch.subject,
        synStatus: this.$refs.synchronizationSearch.statu,
        updateTime: this.$refs.synchronizationSearch.updateTime.toString(),
        id: this.id,
        drugInfoLibCompany: this.$refs.synchronizationSearch.drugInfoLibCompany,
        isCompose: this.$refs.synchronizationSearch.isCompose
      }));
      this.axios.post('/pmph/action/knowledgeLib/syncKnowledgesToPlatforms', data);
    },
    handleSizeChange(value) {
      this.pageSize = value;
      this.confirm();
    },
    confirm: function (isFromChild) {
      if (UTIL.isNotEmpty(isFromChild)) {
        this.currentPage = 0;
      }
      if (UTIL.isEmpty(this.$refs.synchronizationSearch.bindingPlatform)) {
        return;
      }
      this.axios.get('/pmph/action/knowledgeLib/selectSynList', {
        params: {
          currentPage: this.currentPage, // 第几页
          pageNum: this.pageSize, // 每页显示的数据个数
          input: this.$refs.synchronizationSearch.input,
          platform: this.$refs.synchronizationSearch.platform,
          status: this.$refs.synchronizationSearch.statu,
          updateTime: this.$refs.synchronizationSearch.updateTime.toString(),
          id: this.id,
          drugInfoLibCompany: this.$refs.synchronizationSearch.drugInfoLibCompany,
          bindingPlatform: this.$refs.synchronizationSearch.bindingPlatform,
          isCompose: this.$refs.synchronizationSearch.isCompose
        }
      }).then((response) => {
        this.noData = response.data.totalNum <= 0;
        this.synTableData = response.data.tableData
        this.totalNum = response.data.totalNum;
        this.showAdvance = false;
      })
    },
    goBack: function () {
      this.$router.push("/KnowledgeLib")
    },
  },
  mounted: function () {
    if (this.id === CONST.DRUG_INFO_LIB_ID) {
      this.drugInfoLibStatus = true;
    } else {
      this.drugInfoLibStatus = false;
    }
  },
  created() {
    this.id = this.$route.query.id
    this.isToPlatfor = this.$route.query.isToPlatform
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 200;
  }
}
</script>

<style scoped>
.bottom-space {
  margin-bottom: 10px;
}
</style>
